<?php

class Module_Impl_Blog_Select extends Module_Impl_Abstarct_Select  {
	
	protected $_tablename = Config_Table::TABLE_BLOG;	
	protected $_objectname = "Object_Blog";
	
	CONST SQLARRAY_OPTION_JOIN_CATEGORY = 'join_category';
	CONST SQLARRAY_OPTION_JOIN_CATEGORY_SELECT = 'join_category_select';
	CONST SQLARRAY_OPTION_JOIN_USER = 'join_user';
	CONST SQLARRAY_OPTION_JOIN_USER_SELECT = 'join_user_select';
	
	/**
	 * Singleton instance
	 * @var Module_Impl_Blog_Select
	 */
	protected static $_instance = null;
	/**
	 * Singleton instance 
	 * @return Module_Impl_Blog_Select
	 */
	public static function getInstance() {
		if (null === self::$_instance) {
			self::$_instance = new self ( );
		}
		return self::$_instance;
	}		
	
	protected function _getSqlArray($options = array()) {
		$sqlArray = parent::_getSqlArray($options );
		$options = array_merge($this->_options,$options);
		$_join_catogry_info = isset ( $options [self::SQLARRAY_OPTION_JOIN_CATEGORY] ) ? $options [self::SQLARRAY_OPTION_JOIN_CATEGORY] : false;	
		if ($_join_catogry_info) {
			$sqlArray ['join'] [0] ['tablename'] = Config_Table::TABLE_BLOG_CATEGORY;
			$sqlArray ['join'] [0] ['on'] = Config_Table::TABLE_BLOG . '.category_id =' . Config_Table::TABLE_BLOG_CATEGORY . '.id';
			$_join_category_select = isset ( $options [self::SQLARRAY_OPTION_JOIN_CATEGORY_SELECT] ) ? $options [self::SQLARRAY_OPTION_JOIN_CATEGORY_SELECT] : '*';	
			$sqlArray ['join'] [0] ['select'] = $_join_category_select;
		}	
		$_join_user_info = isset ( $options [self::SQLARRAY_OPTION_JOIN_USER] ) ? $options [self::SQLARRAY_OPTION_JOIN_USER] : false;	
		if ($_join_user_info) {
			$sqlArray ['join'] [1] ['tablename'] = Config_Table::TABLE_USER;
			$sqlArray ['join'] [1] ['on'] = Config_Table::TABLE_BLOG . '.user_id =' . Config_Table::TABLE_USER . '.id';
			$_join_user_select = isset ( $options [self::SQLARRAY_OPTION_JOIN_USER_SELECT] ) ? $options [self::SQLARRAY_OPTION_JOIN_USER_SELECT] : '*';	
			$sqlArray ['join'] [1] ['select'] = $_join_user_select;
		}	
		
		return $sqlArray;
	}
	
	public function getLastBlogs($number = 5,$userid = null,$where = null,$orderby = null){
		if(!empty($userid)){
			$where[ Config_Table::TABLE_BLOG.'.user_id = ?'] = $userid;
		}
		$options[self::SQLARRAY_OPTION_LIMIT] = $number;
		$options[self::SQLARRAY_OPTION_OFFSET] = 0;
		$options[self::SQLARRAY_OPTION_ORDERBY] = empty($orderby)?Config_Table::TABLE_BLOG .'.create_at DESC':$orderby;
		$options[self::SQLARRAY_OPTION_SELECT] = array("id","title");		
		return $this->getData($where,$options);
	}
	public function getStarredTopBlogs($number = 5,$userid = null){
		$where[ Config_Table::TABLE_BLOG.'.level = ?'] = Object_Blog::BLOG_STARRED;		
		return $this->getLastBlogs($number,$userid,$where);
	}
	
	public function getTopBlogs($number = 5 ,$userid = null){
		$orderby[0] = Config_Table::TABLE_BLOG .'.remarks_count DESC';
		$orderby[1] = Config_Table::TABLE_BLOG .'.create_at DESC';
		$where[ Config_Table::TABLE_BLOG.'.remarks_count > ?'] = 0;	
		return $this->getLastBlogs($number,$userid,$where,$orderby);
	}
	
}